/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.sys.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.apestar.common.persistence.Page;
import cn.apestar.common.service.CrudService;
import cn.apestar.common.utils.CommonUtil;
import cn.apestar.modules.sys.dao.UserAndInstituionDao;
import cn.apestar.modules.sys.entity.Institution;
import cn.apestar.modules.sys.entity.User;
import cn.apestar.modules.sys.entity.UserAndInstituion;
import cn.apestar.modules.sys.enums.RoleTypeEnum;
import cn.apestar.user.provider.institution.enums.AuditStateEnum;

/**
 * 人员和机构关系Service
 * @author LVH
 * @version 2016-03-25
 */
@Service
@Transactional(readOnly = true)
public class UserAndInstituionService extends CrudService<UserAndInstituionDao, UserAndInstituion> {

    @Override
    public UserAndInstituion get(String id) {

        return super.get(id);
    }

    @Override
    public List<UserAndInstituion> findList(UserAndInstituion userAndInstituion) {

        // userAndInstituion.setState(AuditStateEnum.TG_TG.getValue());
        return super.findList(userAndInstituion);
    }

    @Override
    public Page<UserAndInstituion> findPage(Page<UserAndInstituion> page, UserAndInstituion userAndInstituion) {

        return super.findPage(page, userAndInstituion);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(UserAndInstituion userAndInstituion) {

        if (StringUtils.isEmpty(userAndInstituion.getState()))
            userAndInstituion.setState(AuditStateEnum.TG_TG.getValue());
        if (StringUtils.isEmpty(userAndInstituion.getRoleType()))
            userAndInstituion.setRoleType(RoleTypeEnum.PT.getValue());
        super.save(userAndInstituion);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(UserAndInstituion userAndInstituion) {

        super.delete(userAndInstituion);
    }

    public void findInstitutionTgList(List<Institution> list) {

        if (list.size() == 0)
            return;
        UserAndInstituion userAndInstituion = new UserAndInstituion();
        String id = CommonUtil.getLongString(list, "id").toString();
        userAndInstituion.setState(AuditStateEnum.TG_TG.getValue());
        userAndInstituion.setRoleType(RoleTypeEnum.GLY.getValue());
        userAndInstituion.setCmInstitutionIds(id);
        List<UserAndInstituion> lists = super.findList(userAndInstituion);
        Map map = CommonUtil.listToObjectEqual(lists, "cmInstitutionId");
        for (int i = 0; i < list.size(); i++) {
            Institution m = list.get(i);
            if (map.containsKey(m.getId())) {
                List listUserAndInstitution = (List) map.get(m.getId());
                if (listUserAndInstitution.size() > 0) {
                    UserAndInstituion u = (UserAndInstituion) listUserAndInstitution.get(0);
                    User user = u.getUser();
                    if (user != null) {
                        m.setUserName(user.getName());
                        m.setUserPhone(user.getPhone());
                    }
                }
            }
        }
    }
}